//package com.cykj.gateway.filters;
//
////import cn.dev33.satoken.exception.NotLoginException;
////import cn.dev33.satoken.jwt.SaJwtUtil;
////import cn.dev33.satoken.stp.StpUtil;
////import com.alibaba.fastjson.JSON;
////import com.cykj.common.dto.ResponseDTO;
////import com.cykj.common.entity.AdminDO;
////import com.cykj.common.entity.HrDO;
////import com.cykj.common.entity.RegionDO;
////import com.cykj.common.entity.UserDO;
//import cn.dev33.satoken.stp.StpUtil;
//import com.alibaba.fastjson2.JSON;
//import com.cykj.common.dto.ResponseDTO;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.annotation.Order;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.http.HttpCookie;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.util.List;
//
///**
// * @BelongsProject: smart_recruit_backend
// * @BelongsPackage: com.cykj.gateway.filters
// * @Author: 小白糖糖
// * @CreateTime: 2025-08-30 17:51
// * @Description: TODO
// * @Version: 1.0
// */
//@Component
//public class LoginFilter implements GlobalFilter {
//    @Autowired
//    private RedisTemplate<String, Object> redisTemplate;
//@Value("${sa-token.jwt-secret-key}")
//String secretKey;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//
//        String[] urlWhite = {
//                "/user/login", "/user/enroll",   //用户端白名单
//                "/admin/login",                 //平台端白名单
//                "/hr/login", "/hr/register", "/hr/validateCodeImage"  //企业端白名单
//        };
//
//        String url = exchange.getRequest().getURI().getPath();
//        for (String item : urlWhite) {
//            if (item.equals(url)) {
//                return chain.filter(exchange);
//            }
//        }
//
//        List<String> headers=exchange.getRequest().getHeaders().get("satoken");
//
//        List<HttpCookie> cookies = exchange.getRequest().getCookies().get("satoken");
//
//        String token = "";
//        if (headers != null&& !headers.isEmpty()) {
//            token = headers.get(0);
//            if (redisTemplate.opsForValue().get("satoken:login:token:"+token)!=null) {
//                return chain.filter(exchange);
//            }else {
//                System.err.println(token);
//                System.err.println(redisTemplate.opsForValue().get("satoken:login:token:"+token));
//                return returnTokenError(exchange, "token过期！");
//            }
//        }
//        if (cookies != null && !cookies.isEmpty()) {
//            token = cookies.get(0).getValue();
////            String role = null;
////            Integer id = null;
////            try {
//
//            if (redisTemplate.opsForValue().get("satoken:login:token:"+token)!=null) {
//                return chain.filter(exchange);
//            }else {
//                System.err.println(token);
//                System.err.println(redisTemplate.opsForValue().get("satoken:login:token:"+token));
//                return returnTokenError(exchange, "token过期！");
//            }
////                System.err.println(token);
////                id = (Integer) StpUtil.getLoginIdByToken(token);
////                role = StpUtil.getExtra("role").toString();
//
////            } catch (NotLoginException e) {
////                System.err.println(1111);
////                return returnTokenError(exchange, "token已过期！");
////            } catch (Exception e){
////                e.printStackTrace();
////                System.err.println(22222);
////                return returnTokenError(exchange,"token解析失败！");
////            }
////
////            Object o = redisTemplate.opsForValue().get(role + ":" + id);
////            if (o == null) {
////                return returnTokenError(exchange, "token错误！");
////            }
////            switch (role) {
////                case "user":
////                    UserDO userDO = JSON.parseObject(o.toString(), UserDO.class);
////                    if (!token.equals(userDO.getToken())) {
////                        return returnTokenError(exchange, "token错误！");
////                    }
////                    break;
////                case "hr":
////                    HrDO hrDO = JSON.parseObject(o.toString(), HrDO.class);
////                    if (!token.equals(hrDO.getToken())) {
////                        return returnTokenError(exchange, "token错误！");
////                    }
////                    break;
////                case "admin":
////                    AdminDO adminDO = JSON.parseObject(o.toString(), AdminDO.class);
////                    if (!token.equals(adminDO.getToken())) {
////                        return returnTokenError(exchange, "token错误！");
////                    }
////                default:
////                    break;
////            }
//
//        } else {
//            return returnTokenError(exchange, "请携带token！");
//        }
//    }
//
//    //返回401
//    private Mono<Void> returnTokenError(ServerWebExchange exchange, String msg) {
//        ResponseDTO dto = ResponseDTO.tokenError(msg);
//        DataBuffer wrap = exchange.getResponse().bufferFactory().wrap(JSON.toJSONBytes(dto));
//        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//        exchange.getResponse().getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
//        return exchange.getResponse().writeWith(Mono.just(wrap));
//    }
//
//
//}
